package com.itheima.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.health.dto.SetmealDto;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@Api(value = "套餐管理")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("setmeal/upload")
    @ApiOperation(value = "套餐图片上传",notes = "图片上传到阿里云oss bucketname = jiankangxiang")
    public Result upload(@RequestParam("imgFile")MultipartFile file){
        try{
            //   调用阿里云工具类  将图片 上传oss
            String name = file.getOriginalFilename();//     获取上传文件名称
            String uuidfilename = UploadUtils.generateRandonFileName(name);
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidfilename);//上传文件传输到 oss
            //   图片uuid名称  回送 前端
            //图片信息保存到redis set集合上
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,uuidfilename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuidfilename);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    @PostMapping("setmeal/add")
    @ApiOperation(value = "套餐添加方法",notes = "套餐添加")
    public Result add(@RequestBody SetmealDto setmealDto){
        try{
            setmealService.saveOrUpdateSetmealAndGroup(setmealDto);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询方法",notes = "分页查询")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            Map map = setmealService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @GetMapping("setmeal/findGroupIdAndImgUrlById/{id}")
    @ApiOperation(value = "查询GroupId和图片地址的方法",notes = "根据id查询GroupId和图片地址")
    public Result findGroupIdAndImgUrlById(@PathVariable("id")Integer id){
        try{
            Map map = setmealService.findGroupIdAndImgUrlById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
